//
//  HomePageViewController.m
//  DBAChain
//
//  Created by Evan on 2018/9/4.
//  Copyright © 2018 Evan. All rights reserved.
//

#import "HomePageViewController.h"
#import <Masonry.h>
#import "CommonData.h"
#import "HomePageCardViewCell.h"
#import "PurchaseKJViewController.h"
#import "UIAlertController+Evan.h"
#import "DKProgressHUD.h"
#import "taskHomePageService.h"
#import "NumericalDetailViewController.h"
#import "HomePageHeadView.h"
#import "UILabel+LineSpace.h"

@interface HomePageViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)    UITableView *tableView;
@property (nonatomic,strong)    NSArray   *modelMinerMain;
@property (nonatomic,assign )   NSInteger selectIndex; //资产包类型：0：普通用户，1：合伙人
@property (nonatomic,strong)HomePageHeadView *headView;
@end

@implementation HomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"首页";
    __weak __typeof(self)weakSelf = self;
    self.headView.tabView.selection = ^(NSUInteger index, NSUInteger oldIndex) {
        weakSelf.selectIndex = (unsigned int)index;
        [weakSelf loadMinerMain];
    };
    [self.view addSubview:self.tableView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if (@available(iOS 11.0, *)) {
        UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    [self setBlackStatus:NO];
    
    self.hidesBottomBarWhenPushed = NO;
    if (self.tabBarController.tabBar.hidden == YES) {
        self.tabBarController.tabBar.hidden = NO;
    }
    if (self.navigationController.navigationBar.hidden == NO) {
        self.navigationController.navigationBar.hidden = YES;
    }
    if ([UserCenter shareCenter].isLogined) {
        [self loadMinerMain];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 获取资产包列表
- (void)loadMinerMain{
    //[DKProgressHUD showLoading];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setField:[NSString stringWithFormat:@"%ld",self.selectIndex] value:@"assetType"];
    [taskHomePageService getMinerMain:dict success:^(dataMinerMain *response, BOOL success) {
        //[DKProgressHUD dismiss];
        if (success) {
            self.modelMinerMain = response.data;
            [self.tableView reloadData];
        }else{
            [DKProgressHUD showErrorWithStatus:response.message];
        }
    } failture:^(NSError *error, NSString *strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}

-(HomePageHeadView *)headView{
    if (!_headView) {
        _headView = [HomePageHeadView getTopView];
//        _headView.btnJieShao.tag = _headView.tabView.selectedIndex;
        [_headView.btnJieShao addTarget:self action:@selector(btnIntroduceClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _headView;
}

#pragma mark - tableivew


-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor  = [UIColor clearColor];
        _tableView.scrollEnabled = YES;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
    }
    return _tableView;
}

#pragma mark - UITableViewDelegate

//定义每一个section有多少row
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.modelMinerMain count];
}

//定义有多少section
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

//定义CellRow的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 135;
}

//定义section的Footer的view视图
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

//定义section的Footer的高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

//定义section的header的view视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return self.headView;
}

//定义section的header的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 200;
}

//主体cell的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomePageCardViewCell *cell = [HomePageCardViewCell cellWithTableView:tableView];
    if (self.modelMinerMain.count > 0) {
        dataMinerMainInfo *model = [dataMinerMainInfo mj_objectWithKeyValues:self.modelMinerMain[indexPath.row]];
        cell.lbName.text = model.AssetName;
        cell.lbPrice.text = [NSString stringWithFormat:@"%@元",model.AssetPriceDJ];
        cell.lbCoinNumber.text = [NSString stringWithFormat:@"当前可获得:%@个",model.AssetCoinNumber];
        cell.lbCount.text = [NSString stringWithFormat:@"总量:%@",model.AssetCount];
        cell.lbSurplus.text = [NSString stringWithFormat:@"剩余:%@",model.AssetSurplus];
        if (self.selectIndex == 1) {
            cell.lbPrice.text = [NSString stringWithFormat:@"%@china-A",model.AssetPriceDJ];
            cell.lbName.text = [NSString stringWithFormat:@"  %@  ",model.AssetName];
            cell.lbName.textColor = [UIColor whiteColor];
            cell.lbName.backgroundColor = COLOR_main;
            cell.lbName.layer.masksToBounds = YES;
            cell.lbName.layer.cornerRadius = 11;
            cell.lbName.layer.borderWidth = 1.0f;
            cell.lbName.layer.borderColor = COLOR_main.CGColor;
            cell.lbCoinNumber.text = @"";
            cell.lbCoinNumber.setupmoreColorFont(@"当前可获得:",[UIColor blackColor],12).setupmoreColorFont(model.AssetCoinNumber,COLOR_main,12).setupmoreColorFont(@"个",[UIColor blackColor],12);
        }
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    PurchaseKJViewController *vc = [[PurchaseKJViewController alloc]initWithNibName:@"PurchaseKJViewController" bundle:nil];
    dataMinerMainInfo *model = [dataMinerMainInfo mj_objectWithKeyValues:self.modelMinerMain[indexPath.row]];
    vc.strTitle = model.AssetName;
    vc.sPid = model.ID;
    vc.partner = self.selectIndex;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 介绍

- (void)btnIntroduceClick:(UIButton*)sender{
    NumericalDetailViewController *vc = [NumericalDetailViewController new ];
    //当前点击是普通用户
    if(self.headView.tabView.selectedIndex == 0)
        vc.isHeHuoRen = NO;
    else//当前点击的是会员
        vc.isHeHuoRen = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
